<?php
namespace app\index\controller\user;

use app\common\model\admin\AccountModel;
use app\common\model\paths\ClassTaskModel;
use app\common\model\paths\EmployInfoModel;
use app\common\model\paths\EmployModel;
use app\common\model\paths\TaskChapterModel;
use app\common\model\paths\UserClassModel;
use app\index\controller\BaseIndex;
use think\admin\model\SystemUser;

/**
 * 我的实践项目
 * @package app\index\controller\user
 * @author wangqinghui
 * @DateTime 2024-09-12 11:24:23
 */
class Train extends BaseIndex
{

    // 强制登录
    protected $need_login = true;

    /**
     * 获取我的实践项目列表
     * @author wangqinghui
     * @DateTime 2024-09-13 17:58:27
     * @return void
     */
    public function index()
    {
        $this->title = lang('我的实践项目');
        $this->fetch('index', ['page_size' => 16]);
    }

    /**
     * 我的实战项目列表
     * @author wangqinghui
     * @DateTime 2024-10-09 14:08:06
     * @return void
     */
    public function list() {
        // 搜索内容
        $key = trim($this->request->post('key', ''));
        // 分页
        $page = (int)$this->request->post('page', 1);
        $limit = (int)$this->request->post('limit', 16);

        // 获取我的班级
        $map = [
            ['index_user_id', '=', $this->userInfo['id']],
            ['status', '=', UserClassModel::STATUS_AGREE]// 申请通过
        ];
        $classList = UserClassModel::mk()->field('index_user_id,class_id,platform_id,user_id')->where($map)->select()->toArray();
        $classData = array_column($classList, 'class_id');
        $employ_list = [];
        $total = 0;
        if(!empty($classData)){
            // 获取班级下的课程
            $map = [
                ['class_id', 'in', $classData],
            ];
            $taskList = ClassTaskModel::mk()->where($map)->select()->toArray();
            $taskIds = array_column($taskList, 'task_id');
            if(!empty($taskIds)){
                // 获取课程下的章节
                $map = [
                    ['task_id', 'in', $taskIds],
                ];
                $chapterList = TaskChapterModel::mk()->where($map)->select()->toArray();
                $employ_ids = array_column($chapterList, 'employ_id');
                if(!empty($employ_ids)){
                    // 获取实践项目
                    $map = [
                        ['id',  'in', $employ_ids],
                        ['status', '=', 1]
                    ];
                    if(!empty($key)){
                        $map[] = ['title', 'like', "%{$key}%"];
                    }
                    $total = EmployModel::mk()->where($map)->count();
                    $employ_list = EmployModel::mk()->where($map)->page($page,$limit)->order('id desc')->select()->toArray();
                    if(!empty($employ_list)){
                        // 获取关卡总数
                        $ids = array_column($employ_list, 'id');
                        $map = [
                            ['app_id', 'in', $ids],
                            ['status', '=', 1],// 启用
                            ['pid', '=', 0],
                            ['level', '=', 1] // 关卡
                        ];
                        $chapter_total_list = EmployInfoModel::mk()->field('app_id, count(id) as num')->where($map)->group('app_id')->select()->toArray();
                        $chapter_total = array_column($chapter_total_list, 'num', 'app_id');
                        // 获取课程作者信息
                        $author_ids = array_column($employ_list, 'user_id');
                        $author_data = [];
                        if(!empty($author_ids)){
                            $map = [
                                ['id',  'in', $author_ids],
                                ['status', '=', 1],
                                ['usertype', '=', 'teacher']
                            ];
                            $field = 'id,nickname,headimg';
                            $author_data = SystemUser::mk()->where($map)->column($field, 'id');
                        }

                        // 获取课程所属平台
                        $platform_ids = array_column($employ_list, 'account_id');
                        $platform_data = [];
                        if(!empty($platform_ids)){
                            $map = [
                                ['id',  'in', $platform_ids],
                                ['status', '=', 1],
                                ['module',  'find in set',  AccountModel::MODULE_SCHOOLS]
                            ];
                            $field = 'id,title';
                            $platform_data = AccountModel::mk()->where($map)->column($field, 'id');
                        }
                        foreach ($employ_list as $key => $item){
                            $employ_list[$key]['chapter_total'] = $chapter_total[$item['id']] ?? 0;
                            $employ_list[$key]['auther'] = !empty($author_data[$item['user_id']]['nickname']) ? $author_data[$item['user_id']]['nickname'] : '';
                            $employ_list[$key]['platform'] = !empty($platform_data[$item['account_id']]['title']) ? $platform_data[$item['account_id']]['title'] : '';
                        }
                    }
                }
            }
        }
        $this->success('获取成功', ['total' => $total, 'list' => $employ_list]);
    }
}